package com.syh.jn.scm.service.impl.upstream.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.exception.IllegalParameterException;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.secret.PasswordUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.upstream.entity.system.UpAccount;
import com.syh.jn.scm.domain.upstream.event.system.UpAccountEvent;
import com.syh.jn.scm.domain.upstream.repository.system.UpAccountRepository;
import com.syh.jn.scm.dto.upstream.system.upAccount.UpAccountMto;
import com.syh.jn.scm.dto.upstream.system.upAccount.UpAccountSto;
import com.syh.jn.scm.dto.upstream.system.upAccount.UpAccountVo;
import com.syh.jn.scm.service.api.upstream.system.UpAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UpAccountServiceImpl extends AbstractServiceImpl<UpAccountSto, UpAccountMto, UpAccountVo, UpAccount, Long> implements UpAccountService {

  @Autowired
  private UpAccountRepository upAccountRepository;


  @Override
  protected AggregateRepository<UpAccount, Long> getAggregateRepository() {
    return this.upAccountRepository;
  }

  @Override
  public Long save(UpAccountSto accountSto) {

    UpAccount account = new UpAccount(accountSto);
    return upAccountRepository.add(account);
  }

  @Override
  public void update(UpAccountMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ExtAssert.notNull(mto.getId(), "id为空");
    UpAccount account = upAccountRepository.getById(mto.getId());
    if (account != null) {
      account.modify(mto);
      upAccountRepository.update(account);
    }
  }

  @Override
  public void delete(Long id) {
    ExtAssert.notNull(id, "id为空");
    UpAccount account = upAccountRepository.getById(id);
    UpAccount eventAccount = new UpAccount();
    ExtBeanUtils.copyProperties(account, eventAccount);
    if (account != null) {
      account.destory();
      upAccountRepository.removeById(id);
      this.publicEvent(UpAccountEvent.buildUpAccountDeleted(new Object[]{eventAccount}));
    }
  }


  @Override
  public void changeEable(Long accountId, Short enable) {
    ExtAssert.notNull(accountId, "id为空");
    UpAccount account = upAccountRepository.getById(accountId);
    UpAccount eventAccount = new UpAccount();
    ExtBeanUtils.copyProperties(account, eventAccount);
    if (account != null) {
      account.changeEnable(enable);
      upAccountRepository.update(account);
      if (enable.equals(Short.valueOf("-1")) && eventAccount.getLoginPhoneId() != null) {
        this.publicEvent(UpAccountEvent.buildUpAccountDisabled(new Object[]{eventAccount}));
      }
    }
  }

  @Override
  public void setRole(Long accountId, Long roldId) {
    ExtAssert.notNull(roldId, "id为空");
    UpAccount account = upAccountRepository.getById(accountId);
    if (account != null) {
      account.changeRole(roldId);
      upAccountRepository.update(account);
    }
  }

  @Override
  public UpAccountVo verifyAccountPassword(String loginAccount, String password) {
    ExtAssert.notNull(loginAccount, "loginAccount为空");
    ExtAssert.notNull(password, "密码为空");
    Wrapper wrapper = Wrapper.build();
    wrapper.append(Restrictions.eq("loginAccount", loginAccount));
    UpAccount upAccount = upAccountRepository.getOne(wrapper);
    if (upAccount == null) {
      return null;
    }
    if (PasswordUtils.verify(password, upAccount.getLoginPassword())) {
      return upAccountRepository.getOneView(wrapper);
    } else {
      return null;
    }
  }

  @Override
  public void updataPassword(String loginAccount, String oldPassword, String newPassword) throws IllegalParameterException {
    ExtAssert.notNull(loginAccount, "loginAccount为空");
    ExtAssert.notNull(oldPassword, "旧密码为空");
    ExtAssert.notNull(newPassword, "新密码为空");
    Wrapper wrapper = Wrapper.build();
    wrapper.append(Restrictions.eq("loginAccount", loginAccount));
    UpAccount upAccount = upAccountRepository.getOne(wrapper);
    ExtAssert.notNull(upAccount, "找不到该账户");
    if (PasswordUtils.verify(oldPassword, upAccount.getLoginPassword())) {
      upAccount.setLoginPassword(newPassword);
      upAccountRepository.update(upAccount);
    } else {
      throw new IllegalParameterException("旧密码不正确");
    }
  }

  @Override
  public void bindingLoginPhone(Long id, Long phoneId) {
    UpAccount upAccount = upAccountRepository.getById(id);
    if (upAccount != null && phoneId != null) {
      upAccount.bindingLoginPhone(phoneId);
      upAccountRepository.update(upAccount);
    }
  }

  @Override
  public void untyingLoginPhone(Long id) {
    UpAccount account = upAccountRepository.getById(id);
    UpAccount eventAccount = new UpAccount();
    ExtBeanUtils.copyProperties(account, eventAccount);
    if (account != null) {
      account.untyingLoginPhone();
      upAccountRepository.update(account);
      this.publicEvent(UpAccountEvent.buildUpAccountPhoneUntyed(new Object[]{eventAccount}));
    }
  }

  @Override
  public boolean identityAdmin(Long id) {
    Short admin = upAccountRepository.getAdminById(id);
    if (admin != null && admin.equals(Short.valueOf("1"))) {
      return true;
    }
    return false;
  }
}
